package chagine.core.lock;

import chagine.core.current.CurrentTenant;
import chagine.core.current.CurrentUser;
import chagine.core.lock.redis.RedisReentrantLock;
import chagine.core.lock.redis.util.SeeingLockRedisUtilFactory;

/**
 * @author chengsongping
 * @create 2018-03-21 11:52
 * 用户级的锁，锁的互质由 租户号 + 用户号 + lockKey 来控制
 **/
public class UserLock extends RedisReentrantLock {

    @Deprecated
    public UserLock(String lockKey) {
        this(lockKey, RedisReentrantLock.DEFAULT_LOCK_EXPIRES);
    }

    /**
     * 创建一个租户级的锁
     *
     * @param lockKey    锁用的KEY
     * @param lockExpire 锁超时时间，单位millis
     * @deprecated 推荐 SeeingLockFactory
     */
    @Deprecated
    public UserLock(String lockKey, long lockExpire) {
        this(lockKey, -1, lockExpire);
    }

    /**
     * 创建一个租户级的锁
     *
     * @param lockKey    锁用的KEY
     * @param lockExpire 锁超时时间，单位millis
     * @param dbIndex    redis数据DB
     * @deprecated 推荐 SeeingLockFactory
     */
    @Deprecated
    public UserLock(String lockKey, long lockExpire, int dbIndex) {
        this(lockKey, dbIndex, lockExpire);
    }

    @SuppressWarnings("deprecation")
    private UserLock(String lockKey, int dbIndex, long lockExpire) {
        //noinspection deprecation
        super(CurrentTenant.currentTenant() + "_" + CurrentUser.currentUser() + "_" + lockKey,
                lockExpire,
                SeeingLockRedisUtilFactory.getInstance().getSeeingLockRedisUtil(dbIndex));
    }
}
